package com.gzc.just.play.last.war.sceneserver.ai;

import com.gzc.just.play.last.war.sceneserver.SceneWorkThreadManager;
import com.gzc.just.play.last.war.sceneserver.battle.SceneBattleManager;
import com.gzc.just.play.last.war.sceneserver.config.SceneConfig;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;

import java.util.Collection;
import java.util.concurrent.CompletableFuture;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

/**
 * AIManager类的单元测试
 * 
 * @author AI Assistant
 * @version 1.0.0
 * @since 2025-11-25
 */
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
@DisplayName("AI管理器测试")
class AIManagerTest {

    @Mock
    private SceneConfig mockConfig;
    
    @Mock
    private SceneWorkThreadManager mockWorkThreadManager;
    
    @Mock
    private SceneBattleManager mockBattleManager;
    
    @Mock
    private SceneBattleManager.SceneBattle mockBattle;
    
    @InjectMocks
    private AIManager aiManager;
    
    @BeforeEach
    void setUp() {
        // 配置默认行为
        when(mockConfig.isAiEnabled()).thenReturn(true);
        when(mockConfig.getMiniBattleObjNum()).thenReturn(100);
        when(mockConfig.getAiUpdateInterval()).thenReturn(1000L);
        when(mockBattleManager.getBattle(anyLong())).thenReturn(mockBattle);
    }
    
    @Test
    @DisplayName("测试AI管理器初始化")
    void testAIManagerInitialization() {
        // 验证AI管理器初始化
        assertNotNull(aiManager);
        
        // 验证初始状态
        assertEquals(0, aiManager.getAIEntityCount());
        
        // 验证更新方法不抛出异常
        assertDoesNotThrow(() -> aiManager.updateAI());
    }
    
    @Test
    @DisplayName("测试AI控制器获取")
    void testGetAIController() {
        // 测试获取不存在的AI控制器
        assertNull(aiManager.getAIController(9999L));
        
        // 测试获取所有AI控制器
        Collection<AIManager.AIEntityController> controllers = aiManager.getAllAIControllers();
        assertNotNull(controllers);
        assertEquals(0, controllers.size());
    }
    
    @Test
    @DisplayName("测试移除AI实体")
    void testRemoveAIEntity() {
        // 测试移除不存在的AI实体
        assertFalse(aiManager.removeAIEntity(9999L));
        
        // 验证AI实体数量不变
        assertEquals(0, aiManager.getAIEntityCount());
    }
    
    @Test
    @DisplayName("测试AI禁用状态")
    void testAIDisabledState() {
        // 设置AI禁用
        when(mockConfig.isAiEnabled()).thenReturn(false);
        
        // 验证AIManager初始化后AI管理器处于可用状态（根据配置）
        assertNotNull(aiManager);
        
        // 验证更新AI不应产生异常
        assertDoesNotThrow(() -> aiManager.updateAI());
        
        // 验证实体数量为0
        assertEquals(0, aiManager.getAIEntityCount());
    }
    
    @Test
    @DisplayName("测试AI管理器边界情况")
    void testAIManagerEdgeCases() {
        // 测试空管理器
        assertEquals(0, aiManager.getAIEntityCount());
        assertNull(aiManager.getAIController(9999L));
        assertTrue(aiManager.getAllAIControllers().isEmpty());
        
        // 移除不存在的AI实体
        assertFalse(aiManager.removeAIEntity(9999L));
        
        // 验证更新不会抛出异常
        assertDoesNotThrow(() -> aiManager.updateAI());
    }
    
    @Test
    @DisplayName("测试AI控制器基本功能")
    void testAIControllerBasicFunctionality() {
        // 创建一个模拟的AI控制器
        AIManager.AIEntityController mockController = mock(AIManager.AIEntityController.class);
        when(mockController.isRunning()).thenReturn(true);
        
        // 验证控制器方法不抛出异常
        assertDoesNotThrow(() -> {
            mockController.start();
            mockController.stop();
            mockController.update();
        });
    }
}